package iscas;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

public class LocalMapStateAtomicMerger <K, V> extends LocalMapState<K, V>{
    final String script, func;

    public LocalMapStateAtomicMerger(String name, long period, String func) {
        super(name, null, null, null, period);
        Jedis jedis = RedisConfig.newJedis();
        script = jedis.get("atomicScript");
        jedis.close();
        this.func = func;
    }

    @Override
    public void merge(Jedis jedis) {
        Pipeline pipeline = jedis.pipelined();
        local.keySet().forEach(k -> pipeline.evalsha(script, 4, name, k.toString(), local.remove(k).toString(), func));
        pipeline.sync();
    }
}
